package com.whut.whw.goodsdetail;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.whut.whw.R;

import java.util.List;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/06/20
 *     desc   : “我想换”对话框中我的物品列表
 *     version: 1.0
 * </pre>
 */
public class GoodsListAdapter extends BaseAdapter {
    private Context context;
    private List<SimpleGoodsListEntity> mDatas;
    boolean[] hasChecked;

    public GoodsListAdapter(Context context,List<SimpleGoodsListEntity> mDatas){
        this.context = context;
        this.mDatas = mDatas;
        hasChecked = new boolean[mDatas.size()];
    }

    public List<SimpleGoodsListEntity> getmDatas() {
        return mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(context);
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_exchange_mygoods,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvTitle = (TextView)convertView.findViewById(R.id.tv_exchange_title);
            viewHolder.tvTime = (TextView)convertView.findViewById(R.id.tv_exchange_time);
            viewHolder.rbState = (RadioButton)convertView.findViewById(R.id.rb_exchange_state);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.tvTitle.setText(mDatas.get(position).getTitle());
        viewHolder.tvTime.setText(mDatas.get(position).getTime().toString());
        if(mDatas.get(position).isHasChecked()){
            viewHolder.rbState.setChecked(true);//选中
        }else {
            viewHolder.rbState.setChecked(false);//未选中
        }
        return convertView;
    }

    /*@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(context);
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_exchange_mygoods,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvTitle = (TextView)convertView.findViewById(R.id.tv_exchange_title);
            viewHolder.tvTime = (TextView)convertView.findViewById(R.id.tv_exchange_time);
            viewHolder.rbState = (RadioButton)convertView.findViewById(R.id.rb_exchange_state);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.tvTitle.setText(mDatas.get(position).getTitle());
        viewHolder.tvTime.setText(mDatas.get(position).getTime().toString());
        viewHolder.rbState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            int selectedId = position;
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkedChange(selectedId);
            }
        });
        return convertView;
    }*/

    /**
     * 改变checkId位置处的选中状态
     * @param checkId
     */
    private void checkedChange(int checkId) {
        hasChecked[checkId] = !itemHasChecked(checkId);
    }

    /**
     * 判断位置checkedID处的选中状态
     * @param checkId
     * @return
     */
    private boolean itemHasChecked(int checkId) {
        return hasChecked[checkId];
    }

    class ViewHolder {

        TextView tvTitle;
        TextView tvTime;
        RadioButton rbState;
    }
}
